const {SuccessModel,ErrorModel}=require("../model/resModel")
const login=require("../control/user")
const { set } = require("../db/redis")

const getCookieExpires=()=>{
  const d=new Date()
  d.setTime(d.getTime()+(24*60*60*1000))
  console.log("current Time:"+d.toGMTString());
  
  return d.toGMTString()
}

const handleUserRouter=async (req,res)=>{
  
  const method=req.method
  if(method=="POST"&&req.path=="/api/user/login"){
    let {username,password}=req.body
    // const {username,password}=req.query
    const msg=await login(username,password)
    if(msg.username){
      req.session.username=msg.username
      req.session.realname=msg.realname
      console.log(req.session);
      //同步到redis
      set(req.sessionID,req.session)
      return new SuccessModel({data:msg},{msg:"获取登录成功"})
    }else{
      return new ErrorModel({msg:"获取登录失败"})
    }
    
  }
  //登录验证的测试
  if(method==="GET"&&req.path==="/api/user/login-test"){ 
    if(req.session.username){
      return new SuccessModel({data:req.session},{msg:"登录成功"})
    }else{
      return new ErrorModel({msg:"登录失败"})
    }
  }
}
module.exports=handleUserRouter